package net.shouyong.topbilliards.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;

import net.shouyong.topbilliards.R;
import net.shouyong.topbilliards.bean.BilliardRecord;

import java.util.Arrays;
import java.util.List;

/**
 * A fragment representing a list of Items.
 * <p/>
 * interface.
 */
public class HistoryBillirdrecordFragment extends Fragment {

    private static final String ARG_COLUMN_COUNT = "column-count";
    private static final String ARG_DATE = "date";
    private int mColumnCount = 1;
    private String historyDate;

    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public HistoryBillirdrecordFragment() {
    }

    @SuppressWarnings("unused")
    public static HistoryBillirdrecordFragment newInstance(int columnCount,String date) {
        HistoryBillirdrecordFragment fragment = new HistoryBillirdrecordFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_COLUMN_COUNT, columnCount);
        args.putString(ARG_DATE, date);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
            historyDate = getArguments().getString(ARG_DATE);
        }

        initData();
    }

    private void initData() {
        if (historyDate == null || historyDate.isEmpty()) {
            return;
        }

        AVQuery<BilliardRecord> query2 = AVObject.getQuery(BilliardRecord.class);
        query2.whereExists(BilliardRecord.END_TIME);

        AVQuery<BilliardRecord> query1 = AVObject.getQuery(BilliardRecord.class);
        query1.whereContains(BilliardRecord.START_TIME, historyDate);

        AVQuery<BilliardRecord> query = AVQuery.and(Arrays.asList(query2,query1));
        query.findInBackground(new FindCallback<BilliardRecord>() {


            @Override
            public void done(List<BilliardRecord> list, AVException e) {
                if (e != null) {
                    e.printStackTrace();
                    return;
                }
                if (list == null || list.isEmpty()) {
                    return;
                }
                getRecyclerView().setAdapter(new HistoryBillirdrecordRecyclerViewAdapter(list));
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_history_billirdrecord_list, container, false);

        // Set the adapter
        if (view instanceof RecyclerView) {
            Context context = view.getContext();
            RecyclerView recyclerView = (RecyclerView) view;
            if (mColumnCount <= 1) {
                recyclerView.setLayoutManager(new LinearLayoutManager(context));
            } else {
                recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
            }
//            recyclerView.setAdapter(new HistoryBillirdrecordRecyclerViewAdapter(DummyContent.ITEMS));
        }
        return view;
    }

    public RecyclerView getRecyclerView() {
        return (RecyclerView)getView();
    }
}
